<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="input" />
    <script>
      const input = document.getElementById("input");

      document.body.addEventListener("focusin", () => {
        console.log("body 获取焦点");
      });

      input.addEventListener("focusin", () => {
        console.log("获取焦点");
      });

      input.addEventListener("focusout", () => {
        console.log("失去焦点");
      });

      setTimeout(() => {
        //自动获取焦点，触发获取焦点事件
        input.focus();
      }, 3000);

      setTimeout(() => {
        //自动取消焦点，触发失去焦点事件
        input.blur();
      }, 6000);
    </script>
  </body>
</html>
